package com.xdcplus.web.service.wip.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.exceptions.XdcWebException;
import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.web.common.pojo.dto.wip.WipMarkingBoxDTO;
import com.xdcplus.web.common.pojo.dto.wip.WipMarkingPrdFilterDTO;
import com.xdcplus.web.common.pojo.entity.wip.WipMarkingBox;
import com.xdcplus.web.common.pojo.query.wip.WipMarkingPrdQuery;
import com.xdcplus.web.common.pojo.vo.wip.WipMarkingBoxVO;
import com.xdcplus.web.mapper.tool.ToolInfoMapper;
import com.xdcplus.web.mapper.wip.WipMarkingBoxMapper;
import com.xdcplus.web.mapper.wip.WipMarkingMapper;
import com.xdcplus.web.service.bd.BdOpeService;
import com.xdcplus.web.service.wip.WipMarkingBoxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 打标单片表(WipMarkingPrd)表服务实现类
 *
 * @author Devin.sang
 * @since 2023-05-15 22:05:44
 */
@Slf4j
@Service("wipMarkingBoxService")
public class WipMarkingBoxServiceImpl extends BaseServiceImpl<WipMarkingBoxMapper, WipMarkingBox, WipMarkingBox, WipMarkingBoxVO> implements WipMarkingBoxService {

    @Autowired
    protected WipMarkingBoxMapper wipMarkingBoxMapper;

    @Autowired
    protected WipMarkingMapper wipMarkingMapper;

    @Autowired
    protected BdOpeService bdOpeService;


    @Autowired
    protected ToolInfoMapper toolInfoMapper;


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingPrd(WipMarkingBoxDTO wipMarkingBoxDTO) {

        WipMarkingBox wipMarkingBox = new WipMarkingBox();
        BeanUtil.copyProperties(wipMarkingBoxDTO, wipMarkingBox);

        return this.save(wipMarkingBox);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingPrd(WipMarkingBoxDTO wipMarkingBoxDTO) {

        WipMarkingBox wipMarkingBox = this.getById(wipMarkingBoxDTO.getId());
        if (ObjectUtil.isNull(wipMarkingBox)) {
            log.error("updateWipMarkingPrd() The WipMarkingPrd does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingBoxDTO, wipMarkingBox);

        return this.updateById(wipMarkingBox);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingBox> wipMarkingBoxList) {

        wipMarkingBoxList.forEach(wipMarkingBox -> {
            WipMarkingBox wipMarkingBoxParam = new WipMarkingBox();
            wipMarkingBoxParam.setId(wipMarkingBox.getId());
            if (ObjectUtil.isNotNull(wipMarkingBox.getId())) {
                wipMarkingBox.setId(wipMarkingBox.getId());
                LambdaUpdateWrapper<WipMarkingBox> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingBox::getId, wipMarkingBox.getId());
                update(wipMarkingBox, lambdaUpdate);
            } else {
                save(wipMarkingBox);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingBoxDTO> wipMarkingBoxDTOList) {

        List<WipMarkingBox> wipMarkingBoxList = BeanUtils.copyProperties(wipMarkingBoxDTOList, WipMarkingBox.class);
        return saveOrUpdateBatch(wipMarkingBoxList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingPrdLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingBox wipMarkingBox = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingBox)) {
            log.error("deleteWipMarkingPrd() The WipMarkingPrd does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingPrdByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
        List<String> idList = Arrays.asList(ids.split(","));
        if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
        idList.forEach(id->{
            WipMarkingBox wipMarkingBox = this.getById(id);
            if (ObjectUtil.isNull(wipMarkingBox)) {
                log.error("deleteWipMarkingPrd() The WipMarkingPrd does not exist or has been deleted");
                throw new XdcWebException(ResponseEnum.ERROR);
            }
        });

        return this.removeByIds(idList);
    }

    private List<WipMarkingBox> queryWipMarkingPrdList(WipMarkingPrdFilterDTO wipMarkingPrdFilterDTO) {
        WipMarkingPrdQuery wipMarkingPrdQuery = BeanUtil.copyProperties(wipMarkingPrdFilterDTO, WipMarkingPrdQuery.class);

        return wipMarkingBoxMapper.queryWipMarkingPrd(wipMarkingPrdQuery);
    }

    @Override
    public List<WipMarkingBoxVO> queryWipMarkingPrdVOList(WipMarkingPrdFilterDTO wipMarkingPrdFilterDTO) {
        return this.objectConversion(queryWipMarkingPrdList(wipMarkingPrdFilterDTO));
    }

    @Override
    public PageVO<WipMarkingBoxVO> queryWipMarkingPrd(WipMarkingPrdFilterDTO wipMarkingPrdFilterDTO) {
        PageVO<WipMarkingBoxVO> pageVO = new PageVO<>();

        if (wipMarkingPrdFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingPrdFilterDTO.getCurrentPage(), wipMarkingPrdFilterDTO.getPageSize(),
                    wipMarkingPrdFilterDTO.getOrderType(), wipMarkingPrdFilterDTO.getOrderField());
        }

        List<WipMarkingBox> wipMarkingBoxList = queryWipMarkingPrdList(wipMarkingPrdFilterDTO);

        PageInfo<WipMarkingBox> pageInfo = new PageInfo<>(wipMarkingBoxList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingBoxList));

        return pageVO;
    }

    @Override
    public WipMarkingBoxVO queryWipMarkingPrdById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

}
